package com.itzhigui.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.itzhigui.entity.User;
import com.itzhigui.mapper.UserMapper;
import com.itzhigui.service.UserService;
import com.itzhigui.utils.PageUtil;
import com.itzhigui.utils.ResponseEnum;
import com.itzhigui.utils.ResponseUtil;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author java1412
 * @since 2023-11-09 14:25:05
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseUtil findAll() {
        return ResponseUtil.get(ResponseEnum.USER_ALL_SUCCESS, userMapper.findAll());
    }

    @Override
    public ResponseUtil add(User user) {
        return userMapper.add(user).intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.USER_ADD_SUCCESS) : ResponseUtil.get(ResponseEnum.USER_ADD_FAILURE);
    }

    @Override
    public ResponseUtil apiLogin(User user) {
        // 登录用户
        User loginUser = userMapper.findByUsername(user.getUsername());
        ResponseUtil r = null;
        if (null == loginUser) {
            // 用户名不存在
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_1);
        } else if (!loginUser.getPassword().equals(
                SaSecureUtil.md5BySalt(user.getPassword(), loginUser.getSalt()))
        ) {
            // 提交的密码和数据库的密码不相同，登录失败
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_2);
        } else if (loginUser.getRole().intValue() != 1) {
            // 用户名和密码是匹配，判断角色不匹配
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_3);
        } else {
            // 登录成功
            // 密码设置成null
            loginUser.setPassword(null);
            loginUser.setSalt(null);
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_SUCCESS, loginUser);
        }
        return r;
    }

    @Override
    public ResponseUtil addApi(User user) {
        //=======================================设置user中相关参数，有些将来从页面提交=================================================
        user.setEmail("110110@qq.com"); // 设置邮箱
        user.setQuestion("王乐晨喜欢哪个女明星?");   // 设置找回密码的问题
        user.setAnswer("我喜欢AB");
        user.setRole(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        user.setSalt(salt);
        // 借助盐给密码加密
        String oldPassword = user.getPassword();
        user.setPassword(SaSecureUtil.md5BySalt(oldPassword, salt));
        //=======================================设置user中相关参数，有些将来从页面提交=================================================
        return userMapper.add(user).intValue() > 0 ?
                ResponseUtil.get(ResponseEnum.USER_ADD_SUCCESS) : ResponseUtil.get(ResponseEnum.USER_ADD_FAILURE);
    }

    @Override
    public ResponseUtil findAdminByName(User user) {
        ResponseUtil r = null;
        User loginUser = userMapper.findByUsername(user.getUsername());
        if (null == loginUser) {
            // 用户名不存在
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_1);
        } else if (!loginUser.getPassword().equals(
                SaSecureUtil.md5BySalt(user.getPassword(), loginUser.getSalt()))
        ) {
            // 提交的密码和数据库的密码不相同，登录失败
            r = ResponseUtil.get(ResponseEnum.USER_API_LOGIN_2);
        } else if (loginUser.getRole().intValue() != 0) {
            r = ResponseUtil.get(ResponseEnum.USER_ADMIN_LOGIN_ERROR_ROLE);
        } else {
            loginUser.setPassword(null);
            loginUser.setSalt(null);
            r = ResponseUtil.get(ResponseEnum.USER_ADMIN_LOGIN_SUCCESS, loginUser);
        }
        return r;
    }

    @Override
    public ResponseUtil find(Integer page, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        Integer startIndexNo = (page - 1) * pageSize;
        map.put("startIndexNo", startIndexNo);
        map.put("pageSize", pageSize);
        PageUtil<User> pageUtil = new PageUtil<>();
        pageUtil.setCurrentPageNo(page);
        pageUtil.setPageSize(pageSize);
        Integer totalCount = userMapper.findCount(map);
        pageUtil.setTotalCount(totalCount);
        List<User> data = userMapper.find(map);
        pageUtil.setData(data);
        return ResponseUtil.get(ResponseEnum.USER_FIND_SUCCESS, pageUtil);
    }
}
